/*
*  用户事件   keyboard   touch
*
*/

var Event = 
{
    _cur : 0,
    keyStat: {
        jump : 0,
        down : 0,
        left : 0,
        right: 0,
        light_boxing : 0,
        light_kick : 0,
        heavy_boxing : 0,
        heavy_kick : 0
        
    },
    eventList : [],
    getKeyName(code){
        var name = 0;
        switch(code){
            case 87:
                name = "jump";
                break;
            case 83:
                name = "down";
                break;
            case 65:
                name = "left";
                break;
            case 68:
                name = "right";
                break;
            case 74:
                name = "light_boxing";
                break;
            case 75:
                name = "light_kick";
                break;
            case 73:
                name = "heavy_boxing";
                break;
            case 79:
                name = "heavy_kick";
                break;
        }
        return name;
    },
    
    updateEvent(){
        var it = this;
        for(var x in it.eventList){
            var v = it.eventList[x];
            if((+new Date) - v.addtime >=200 && v.name != it._cur){
                it.eventList.splice(x,1);
            }
        }
    },
    addEvent(code){
        var it = this,name=it.getKeyName(code);
        
        if(name){
            it.keyStat[name] = 1;
            it.eventList.push({name:name,addtime:( +new Date )})
        }
        
        
    },
    removeEvent(code){
        var it = this,name=it.getKeyName(code);
        
        if(name){
            it.keyStat[name] = 0;
        }
    },
    getPressKey(){
        var it = this;
        var ret = [];
        for(var x in it.keyStat){
            if(it.keyStat[x]) ret.push({name:x,addtime:(+new Date)})
        }
        return ret;
    },
    keydownNum : 0,
    

    // 绑定事件  可以keyboard  touch  
    addEventListener(){
        var it = this;
        document.addEventListener("keydown",function(e){
            it.addEvent(e.keyCode);
            
            it.keydownNum++;
        })
        document.addEventListener("keyup",function(e){
            it.removeEvent(e.keyCode)
            it.keydownNum--;
        })
    }
        
}